package com.ruoyi.cderp.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.domain.SettleCheckSheetDetail;
import com.ruoyi.cderp.mapper.SettleCheckSheetMapper;
import com.ruoyi.cderp.domain.SettleCheckSheet;
import com.ruoyi.cderp.service.ISettleCheckSheetService;

/**
 * 供应商对账单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-11
 */
@Service
public class SettleCheckSheetServiceImpl implements ISettleCheckSheetService
{
    @Autowired
    private SettleCheckSheetMapper settleCheckSheetMapper;

    LoginUser loginUser = null;

    @Override
    public List<SettleCheckSheetDetail> selectFeeSheetCheckDetailList(Date startDate, Date endDate, String supplierId) {
        List<SettleCheckSheetDetail> list = new ArrayList<>();
        //供应商费用
        List<SettleCheckSheetDetail> sheetFeeSheetList = settleCheckSheetMapper.selectSettleSheetSheet(startDate, endDate, supplierId);
        list.addAll(sheetFeeSheetList);
        //供应商预收款
        List<SettleCheckSheetDetail> selectSettlePreSheet = settleCheckSheetMapper.selectSettlePreSheet(startDate, endDate, supplierId);
        list.addAll(selectSettlePreSheet);
//        //供应商收货
//        List<SettleCheckSheetDetail> selectReceiveSheet = settleCheckSheetMapper.selectReceiveSheetList(startDate, endDate, supplierId);
//        list.addAll(selectReceiveSheet);
//        //供应商采购退货
//        List<SettleCheckSheetDetail> purchaseReturnList = settleCheckSheetMapper.selectPurchaseReturnList(startDate, endDate, supplierId);
//        list.addAll(purchaseReturnList);
        return list;
    }

    /**
     * 查询供应商对账单
     *
     * @param id 供应商对账单主键
     * @return 供应商对账单
     */
    @Override
    public SettleCheckSheet selectSettleCheckSheetById(String id)
    {
        SettleCheckSheet settleCheckSheets = settleCheckSheetMapper.selectSettleCheckSheetById(id);
        List<SettleCheckSheetDetail> SettleCheckSheetDetails = new ArrayList<>();
        List<SettleCheckSheetDetail> SettleCheckSheetDetail1 = settleCheckSheetMapper.selectSettleCheckSheetDetailList(id);
        Set<Long> num = new HashSet<>();
        for (SettleCheckSheetDetail settleCheckSheetDetail1 : SettleCheckSheetDetail1){
            num.add(settleCheckSheetDetail1.getBizType());

        }
        for(Long num1 : num){
            if(num1 == 1){
                SettleCheckSheetDetails.addAll(settleCheckSheetMapper.selectSettleSheetSheetDetail(id));
            }
            if(num1 == 2){
                SettleCheckSheetDetails.addAll(settleCheckSheetMapper.selectSettlePreSheetDetail(id));
            }
//            if(num1 == 3){
//                SettleCheckSheetDetails.addAll(settleCheckSheetMapper.selectReceiveSheetDetail(id));
//            }
//            if(num1 == 4){
//                SettleCheckSheetDetails.addAll(settleCheckSheetMapper.selectPurchaseReturnDetail(id));
//            }
        }
        settleCheckSheets.setSettleCheckSheetDetailList(SettleCheckSheetDetails);
        return settleCheckSheets;
    }

    /**
     * 查询供应商对账单列表
     *
     * @param settleCheckSheet 供应商对账单
     * @return 供应商对账单
     */
    @Override
    public List<SettleCheckSheet> selectSettleCheckSheetList(SettleCheckSheet settleCheckSheet)
    {
        return settleCheckSheetMapper.selectSettleCheckSheetList(settleCheckSheet);
    }

    /**
     * 新增供应商对账单
     *
     * @param settleCheckSheet 供应商对账单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSettleCheckSheet(SettleCheckSheet settleCheckSheet)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        settleCheckSheet.setCreateById(loginUser.getUser().getUserId().toString());
        settleCheckSheet.setCreateBy(loginUser.getUser().getUserName());
        settleCheckSheet.setCreateTime(DateUtils.getNowDate());
        int rows = settleCheckSheetMapper.insertSettleCheckSheet(settleCheckSheet);
        insertSettleCheckSheetDetail(settleCheckSheet);
        return rows;
    }

    /**
     * 修改供应商对账单
     *
     * @param settleCheckSheet 供应商对账单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSettleCheckSheet(SettleCheckSheet settleCheckSheet)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        settleCheckSheet.setUpdateById(loginUser.getUser().getUserId().toString());
        settleCheckSheet.setUpdateBy(loginUser.getUser().getUserName());
        settleCheckSheet.setUpdateTime(DateUtils.getNowDate());
        settleCheckSheetMapper.deleteSettleCheckSheetDetailBySheetId(settleCheckSheet.getId());
        insertSettleCheckSheetDetail(settleCheckSheet);
        return settleCheckSheetMapper.updateSettleCheckSheet(settleCheckSheet);
    }

    /**
     * 批量删除供应商对账单
     *
     * @param ids 需要删除的供应商对账单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSettleCheckSheetByIds(String[] ids)
    {
        settleCheckSheetMapper.deleteSettleCheckSheetDetailBySheetIds(ids);
        return settleCheckSheetMapper.deleteSettleCheckSheetByIds(ids);
    }

    /**
     * 删除供应商对账单信息
     *
     * @param id 供应商对账单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSettleCheckSheetById(String id)
    {
        settleCheckSheetMapper.deleteSettleCheckSheetDetailBySheetId(id);
        return settleCheckSheetMapper.deleteSettleCheckSheetById(id);
    }

    /**
     * 新增供应商对账单明细信息
     *
     * @param settleCheckSheet 供应商对账单对象
     */
    public void insertSettleCheckSheetDetail(SettleCheckSheet settleCheckSheet)
    {
        List<SettleCheckSheetDetail> settleCheckSheetDetailList = settleCheckSheet.getSettleCheckSheetDetailList();
        String id = settleCheckSheet.getId();
        if (StringUtils.isNotNull(settleCheckSheetDetailList))
        {
            List<SettleCheckSheetDetail> list = new ArrayList<SettleCheckSheetDetail>();
            for (SettleCheckSheetDetail settleCheckSheetDetail : settleCheckSheetDetailList)
            {

                settleCheckSheetDetail.setSheetId(id);
                //随机生成组合商品编号
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String formattedDate = sdf.format(new Date());
                Random random = new Random();
                int randomNumber = 10000000 + random.nextInt(90000000); // 生成8位数随机数
                settleCheckSheetDetail.setId("170" + formattedDate+randomNumber);
                list.add(settleCheckSheetDetail);
            }
            if (list.size() > 0)
            {
                settleCheckSheetMapper.batchSettleCheckSheetDetail(list);
            }
        }
    }
}
